# -*-coding:utf-8-*-
'''
沙箱环境	http://gw.api.tbsandbox.com/router/rest	https://gw.api.tbsandbox.com/router/rest
正式环境	http://gw.api.taobao.com/router/rest	https://eco.taobao.com/router/rest
海外环境	http://api.taobao.com/router/rest	https://api.taobao.com/router/rest
'''
import time
import hmac


# http://open.taobao.com/docs/doc.htm?spm=a3142.7395905.4.22.80kC3e&articleId=101617&docType=1&treeId=1
class Wxnado(object):
    def __init__(self, appkey, secret):
        self.appkey = appkey
        self.secret = secret

    def __build_common_params(self, sign_method='hmac'):
        params = {}
        params['app_key'] = self.appkey
        params['sign_method'] = sign_method
        params['timestamp'] = time.strftime('%Y-%m-%d %H:%M:%S')
        params['format'] = 'json'
        params['v'] = '2.0'
        return params

    def send_sms(self, to, sign_name, temp_code, temp_param, cookie=None):
        params = self.__build_common_params()
        params['method'] = 'alibaba.aliqin.fc.sms.num.send'
        params['sms_type'] = 'normal'
        params['sign'] = 'pending'
        params['extend'] = cookie
        params['sms_free_sign_name'] = sign_name
        params['sms_template_code'] = temp_code
        params['sms_param'] = temp_param
        params['rec_num'] = to
        keys = params.keys()
        keys.sort()
        params_string = ''
        for key in keys:
            params_string = params_string + key + str(params[key])
        # digestmod default to hashlib.md5
        h = hmac.new(self.secret, params_string)
        h.hexdigest()
